<script name="CallbackBind" lang="ts" setup>
import Message from '@/components/message';
// import router from '@/router';
import useStore from '@/store';
import { QQUserInfo, QQUserInfoRes } from '@/types/user'
import { useCountDown } from '@/utils/hooks';
import { codeRule, mobileRule } from '@/utils/validate';
import { useField, useForm } from 'vee-validate';
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router';
const route = useRoute()
const router = useRouter()
const { user } = useStore()
const { count, start } = useCountDown()
const qqInfo = ref<QQUserInfo>({} as QQUserInfo)
const props = defineProps({
  openId: {
    type: String,
    required: true
  }
})
// 1. 判断QQ是否登录
if (QC.Login.check()) {
  // 2. 获取QQ用户信息
  QC.api('get_user_info').success((res: QQUserInfoRes) => {
    // console.log(res)
    qqInfo.value = res.data
  })
}
const { validate } = useForm({
  validationSchema: {
    mobile: mobileRule,
    code: codeRule
  }
})
const { value: mobile, errorMessage: mobileErrormessage, validate: mobileValidate } = useField<string>('mobile')
const { value: code, errorMessage: codeErrormessage } = useField<string>('code')
const mobileref = ref<null | HTMLInputElement>(null)

const sendCode = async () => {
  //倒计时期间禁止点击
  if (count.value > 0) return
  // 发送验证码之前校验手机号
  const result = await mobileValidate()
  if (!result.valid) {
    mobileref.value?.focus()
    return
  }
  // console.log('发送验证码');
  start()
  // 4. 发送短信验证码
  await user.sendQQBindMsg(mobile.value)
  Message.success('验证码已发送')
}
//已有账号绑定 绑定qq和手机号关联
const bindQQ = async () => {
  //预校验
  const res = await validate()
  // console.log(res);
  if (!res.valid) return
  //发送请求
  await user.qqBindLogin(props.openId, mobile.value, code.value)
  //提示绑定成功
  Message.success('手机号与QQ绑定成功')
  // 跳转到首页
  // router.push('/')
  if (route.query.redirectUrl) {
    router.push(route.query.redirectUrl as string)
  } else {
    router.push('/')
  }
}
</script>
<template>
  <div class="xtx-form">
    <div class="user-info">
      <img :src="qqInfo.figureurl_1" alt="" />
      <p>Hi,{{ qqInfo.nickname }} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input ref="mobileref" class="input" type="text" v-model="mobile" placeholder="绑定的手机号" />
      </div>
      <div class="error" v-if="mobileErrormessage">{{ mobileErrormessage }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input class="input" type="text" v-model="code" placeholder="短信验证码" />
        <span class="code" @click="sendCode" :class="{ not: count > 0 }">{{ count > 0 ? `重新发送(${count})` : '发送验证码'
        }}</span>
      </div>
      <div class="error" v-if="codeErrormessage">{{ codeErrormessage }}</div>
    </div>
    <a href="javascript:;" class="submit" @click="bindQQ">立即绑定</a>
  </div>
</template>

<style scoped lang="less">
.not {
  cursor: not-allowed !important;
}

.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;

  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }

  p {
    padding-left: 10px;
  }
}

.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;

  &:hover {
    cursor: pointer;
  }
}
</style>
